package com.springboot.uav.controller;

import com.springboot.uav.dto.EditProfileDto;
import com.springboot.uav.dto.UpdateSexDto;
import com.springboot.uav.entity.Address;
import com.springboot.uav.entity.Result;
import com.springboot.uav.mapper.UserMapper;
import com.springboot.uav.service.MyPageAddressService;
import com.springboot.uav.service.UserService;
import com.springboot.uav.vo.MyPageUserVo;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/mypage")
@Tag(name = "用户接口", description = "用户管理相关接口")
public class MyPageController {

    @Autowired
    private UserService userService;

    /**
     * 编辑用户个人信息-电话号码
     * @param editProfileDto
     * @return
     */
    @Operation(summary = "编辑用户个人信息", description = "更新用户的电话号码")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "电话号码更新成功"),
            @ApiResponse(responseCode = "400", description = "请求参数错误"),
            @ApiResponse(responseCode = "401", description = "用户未授权"),
            @ApiResponse(responseCode = "500", description = "内部服务器错误")
    })
    @PostMapping("/editProfile")
    public Result editUserProfile(@RequestBody EditProfileDto editProfileDto) {
        boolean updated = userService.updatePhoneNumber(editProfileDto.getUserId(), editProfileDto.getPhoneNumber());
        if (updated) {
            return Result.success("电话号码更新成功");
        } else {
            return Result.error(400,"电话号码更新失败");
        }
    }

    /**
     * 获取用户电话号码
     * @param userId
     * @return
     */
    @Operation(summary = "获取用户电话号码", description = "根据用户ID获取用户的电话号码")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "成功返回电话号码"),
            @ApiResponse(responseCode = "401", description = "未授权访问"),
            @ApiResponse(responseCode = "404", description = "用户未找到"),
            @ApiResponse(responseCode = "500", description = "内部服务器错误")
    })
    @GetMapping("/phone/{userId}")
    public Result getPhoneNumber(@PathVariable Integer userId) {
        String phoneNumber = userService.getPhoneNumber(userId);
        if (phoneNumber != null) {
            return Result.success(phoneNumber);
        } else {
            return Result.error(404, "用户未找到");
        }
    }

    @Autowired
    private MyPageAddressService myPageaddressService;

    /**
     * 获取用户地址列表
     * @param userId
     * @return
     */
    @Operation(summary = "获取用户地址列表", description = "获取用户的所有地址信息")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "成功返回地址列表"),
            @ApiResponse(responseCode = "401", description = "未授权访问"),
            @ApiResponse(responseCode = "500", description = "内部服务器错误")
    })
    @GetMapping("/addresses")
    public Result getAddresses(@RequestParam Integer userId) {
        List<Address> addresses = myPageaddressService.getAddressListByUserId(userId);
        return Result.success(addresses);
    }

    /**
     * 增加地址信息
     * @param address
     * @return
     */
    @Operation(summary = "增加地址信息", description = "用户可以增加新的地址信息")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "地址增加成功"),
            @ApiResponse(responseCode = "400", description = "请求参数错误"),
            @ApiResponse(responseCode = "401", description = "未授权访问"),
            @ApiResponse(responseCode = "500", description = "内部服务器错误")
    })
    @PostMapping("/address")
    public Result addAddress(@RequestBody Address address) {

        boolean saved = myPageaddressService.saveAddress(address);
        return saved ? Result.success("地址增加成功") : Result.error(400,"地址增加失败");
    }

    /**
     * 修改地址信息
     * @param address
     * @return
     */
    @Operation(summary = "修改地址信息", description = "用户可以修改已有的地址信息")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "地址修改成功"),
            @ApiResponse(responseCode = "400", description = "请求参数错误"),
            @ApiResponse(responseCode = "401", description = "未授权访问"),
            @ApiResponse(responseCode = "500", description = "内部服务器错误")
    })
    @PutMapping("/address")
    public Result updateAddress(@RequestBody Address address) {
        boolean updated = myPageaddressService.updateById(address);
        return updated ? Result.success("地址修改成功") : Result.error(400,"地址修改失败");
    }

    /**
     * 删除地址信息
     * @param addId
     * @return
     */
    @Operation(summary = "删除地址信息", description = "用户可以删除某个地址信息")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "地址删除成功"),
            @ApiResponse(responseCode = "400", description = "请求参数错误"),
            @ApiResponse(responseCode = "401", description = "未授权访问"),
            @ApiResponse(responseCode = "500", description = "内部服务器错误")
    })
    @DeleteMapping("/address/{addId}")
    public Result deleteAddress(@PathVariable Integer addId) {
        boolean removed = myPageaddressService.deleteAddressById(addId);
        return removed ? Result.success("地址删除成功") : Result.error(400,"地址删除失败");
    }

    /**
     * 修改用户性别
     * @param updateSexDto
     * @return
     */
    @Operation(summary = "修改用户性别", description = "根据用户ID修改性别")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "修改成功"),
            @ApiResponse(responseCode = "400", description = "请求参数错误"),
            @ApiResponse(responseCode = "404", description = "用户未找到")
    })
    @PutMapping("/sex")
    public Result updateSex(@RequestBody UpdateSexDto updateSexDto) {
        boolean updated = userService.updateSex(updateSexDto.getUserId(), updateSexDto.getSex());
        if (updated) {
            return Result.success("性别修改成功");
        } else {
            return Result.error(400, "性别修改失败");
        }
    }

    /**
     * 获取用户性别
     * @param userId
     * @return
     */
    @Operation(summary = "获取用户性别", description = "根据用户ID获取性别")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "成功返回性别"),
            @ApiResponse(responseCode = "404", description = "用户未找到")
    })
    @GetMapping("/sex/{userId}")
    public Result getSex(@PathVariable Integer userId) {
        Integer sex = userService.getSexById(userId);
        if (sex != null) {
            return Result.success(sex);
        } else {
            return Result.error(404, "用户未找到");
        }
    }

    /**
     * 修改用户昵称
     * @param userId
     * @param newNickname
     * @return
     */
    @Operation(summary = "修改用户昵称", description = "允许用户更新其昵称")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "昵称更新成功"),
            @ApiResponse(responseCode = "400", description = "请求参数错误"),
            @ApiResponse(responseCode = "401", description = "用户未授权"),
            @ApiResponse(responseCode = "500", description = "内部服务器错误")
    })
    @PutMapping("/nickname")
    public Result updateNickname(@RequestParam Integer userId, @RequestParam String newNickname) {
        boolean updated = userService.updateNickname(userId, newNickname);
        return updated ? Result.success("昵称更新成功") : Result.error(400, "昵称更新失败");
    }

    /**
     * 修改用户密码
     * @param userId
     * @param oldPassword
     * @param newPassword
     * @return
     */
    @Operation(summary = "修改用户密码", description = "允许用户更新其密码")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "密码更新成功"),
            @ApiResponse(responseCode = "400", description = "请求参数错误"),
            @ApiResponse(responseCode = "401", description = "用户未授权"),
            @ApiResponse(responseCode = "500", description = "内部服务器错误")
    })
    @PutMapping("/password")
    public Result updatePassword(@RequestParam Integer userId, @RequestParam String oldPassword, @RequestParam String newPassword) {
        boolean updated = userService.updatePassword(userId, oldPassword, newPassword);
        return updated ? Result.success("密码更新成功") : Result.error(400, "密码更新失败，可能是旧密码错误");
    }

    /**
     * 获取用户信息 电话号码、昵称、性别
     * @param userId
     * @return
     */
    @Autowired
    private UserMapper userMapper;
    @Operation(summary = "获取用户信息", description = "根据用户ID获取用户的昵称、性别和电话号码")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "成功返回用户信息"),
            @ApiResponse(responseCode = "404", description = "用户未找到")
    })
    @GetMapping("/info/{userId}")
    public Result getUserInfo(@PathVariable Integer userId) {
        MyPageUserVo userVo = userMapper.getUserInfo(userId);
        if (userVo != null) {
            return Result.success(userVo);
        } else {
            return Result.error(404, "用户未找到");
        }
    }
}
